home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectInput / DIConfig / flexmsgbox.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  3.8 KB  |  184 lines

  1. //-----------------------------------------------------------------------------
  2. // File: flexMsgBox.cpp
  3. //
  4. // Desc: Implements a message box control similar to Windows message box
  5. //       without the button.  CFlexMsgBox is derived from CFlexWnd.
  6. //
  7. // Copyright (C) 1999-2001 Microsoft Corporation. All Rights Reserved.
  8. //-----------------------------------------------------------------------------
  9.  
  10. #include "common.hpp"
  11.  
  12. CFlexMsgBox::CFlexMsgBox() :
  13.     m_hWndNotify(NULL),
  14.     m_rgbText(RGB(255,255,255)),
  15.     m_rgbBk(RGB(0,0,0)),
  16.     m_rgbSelText(RGB(0,0,255)),
  17.     m_rgbSelBk(RGB(0,0,0)),
  18.     m_rgbFill(RGB(0,0,255)),
  19.     m_rgbLine(RGB(0,255,255)),
  20.     m_hFont(NULL),
  21.     m_tszText(NULL)
  22. {
  23. }
  24.  
  25. CFlexMsgBox::~CFlexMsgBox()
  26. {
  27.     delete[] m_tszText;
  28. }
  29.  
  30. HWND CFlexMsgBox::Create(HWND hParent, const RECT &rect, BOOL bVisible)
  31. {
  32.     m_bSent = FALSE;
  33.     return CFlexWnd::Create(hParent, rect, bVisible);
  34. }
  35.  
  36. void CFlexMsgBox::SetText(LPCTSTR tszText)
  37. {
  38.     LPTSTR tszTempText = NULL;
  39.  
  40.     if (tszText)
  41.     {
  42.         tszTempText = new TCHAR[_tcslen(tszText) + 1];
  43.         if (!tszTempText) return;
  44.         _tcscpy(tszTempText, tszText);
  45.     }
  46.  
  47.     delete[] m_tszText;
  48.     m_tszText = tszTempText;
  49. }
  50.  
  51. void CFlexMsgBox::SetFont(HFONT hFont)
  52. {
  53.     m_hFont = hFont;
  54.  
  55.     if (m_hWnd == NULL)
  56.         return;
  57.  
  58.     Invalidate();
  59. }
  60.  
  61. void CFlexMsgBox::SetColors(COLORREF text, COLORREF bk, COLORREF seltext, COLORREF selbk, COLORREF fill, COLORREF line)
  62. {
  63.     m_rgbText = text;
  64.     m_rgbBk = bk;
  65.     m_rgbSelText = seltext;
  66.     m_rgbSelBk = selbk;
  67.     m_rgbFill = fill;
  68.     m_rgbLine = line;
  69.     Invalidate();
  70. }
  71.  
  72. void CFlexMsgBox::SetRect()
  73. {
  74.     if (m_hWnd == NULL)
  75.         return;
  76.  
  77.     RECT rect = GetRect();
  78.     SetWindowPos(m_hWnd, NULL, rect.left, rect.top, rect.right, rect.bottom, SWP_NOZORDER | SWP_NOMOVE);
  79. }
  80.  
  81. RECT CFlexMsgBox::GetRect(const RECT &rect)
  82. {
  83.     int h = GetTextHeight(m_hFont);
  84.     RECT ret = {rect.left, rect.top, rect.right, rect.top + h + 2};
  85.     return ret;
  86. }
  87.  
  88. RECT CFlexMsgBox::GetRect()
  89. {
  90.     RECT rect;
  91.     GetClientRect(&rect);
  92.     return GetRect(rect);
  93. }
  94.  
  95. void CFlexMsgBox::OnPaint(HDC hDC)
  96. {
  97.     HDC hBDC = NULL, hODC = NULL;
  98.     CBitmap *pbm = NULL;
  99.  
  100.     if (!InRenderMode())
  101.     {
  102.         hODC = hDC;
  103.         pbm = CBitmap::Create(GetClientSize(), RGB(0,0,0), hDC);
  104.         if (pbm != NULL)
  105.         {
  106.             hBDC = pbm->BeginPaintInto();
  107.             if (hBDC != NULL)
  108.             {
  109.                 hDC = hBDC;
  110.             }
  111.         }
  112.     }
  113.  
  114.     InternalPaint(hDC);
  115.  
  116.     if (!InRenderMode())
  117.     {
  118.         if (pbm != NULL)
  119.         {
  120.             if (hBDC != NULL)
  121.             {
  122.                 pbm->EndPaintInto(hBDC);
  123.                 pbm->Draw(hODC);
  124.             }
  125.             delete pbm;
  126.         }
  127.     }
  128.  
  129.     // Post reset message to config window now that the msg window is shown, if
  130.     // we haven't done so.
  131.     if (!m_bSent)
  132.     {
  133.         HWND hParentWnd = GetParent(m_hWnd);
  134.         PostMessage(hParentWnd, WM_CFGUIRESET, 0, 0);
  135.     }
  136.     // Flag it that we've sent the message.
  137.     m_bSent = TRUE;
  138. }
  139.  
  140. void CFlexMsgBox::InternalPaint(HDC hDC)
  141. {
  142.     HGDIOBJ hBrush = (HGDIOBJ)CreateSolidBrush(m_rgbBk);
  143.     if (hBrush != NULL)
  144.     {
  145.         HGDIOBJ hOldBrush = SelectObject(hDC, hBrush);
  146.  
  147.         // Create pen for check box
  148.         HGDIOBJ hPen = (HGDIOBJ)CreatePen(PS_SOLID, 1, m_rgbLine);
  149.         if (hPen != NULL)
  150.         {
  151.             HGDIOBJ hOldPen = SelectObject(hDC, hPen);
  152.  
  153.             // Erase the background and also draw border
  154.             RECT client;
  155.             GetClientRect(&client);
  156.             Rectangle(hDC, client.left, client.top, client.right, client.bottom);
  157.  
  158.             InflateRect(&client, -1, -1);
  159.  
  160. //            SetBkMode(hDC, TRANSPARENT);
  161.  
  162.             // Draw the message text
  163.             SetTextColor(hDC, m_rgbText);
  164.             SetBkColor(hDC, m_rgbBk);
  165.             DrawText(hDC, m_tszText, -1, &client, DT_CENTER|DT_VCENTER|DT_NOPREFIX|DT_SINGLELINE);
  166.  
  167.             SelectObject(hDC, hOldPen);
  168.             DeleteObject(hPen);
  169.         }
  170.  
  171.         SelectObject(hDC, hOldBrush);
  172.         DeleteObject(hBrush);
  173.     }
  174. }
  175.  
  176. void CFlexMsgBox::Notify(int code)
  177. {
  178.     if (!m_hWndNotify)
  179.         return;
  180.  
  181.     PostMessage(m_hWndNotify, WM_FLEXCHECKBOX,
  182.         (WPARAM)code, (LPARAM)(LPVOID)this);
  183. }
  184.